package cn.book.org.service.impl;


import cn.book.basic.service.impl.BaseServiceImpl;
import cn.book.org.domain.Department;
import cn.book.org.mapper.DepartmentMapper;
import cn.book.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description: TODO
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/21 17:55
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;



    /**
    * @Title: save
    * @Description: 添加
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/21 17:32
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getSn() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
    * @Title: update
    * @Description: 修改
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/21 17:32
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getSn() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
    * @Title: tree
    * @Description: TODO
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/26 15:44
    * @Parameters: []
    * @Return java.util.List<cn.book.org.domain.Department>
    */
    @Override
    public List<Department> tree() {
        // 查出全部数据
        List<Department> depts = departmentMapper.loadAll();
        // 创建对象收集数据
        List<Department> deptTree = new ArrayList<Department>();
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts) {
            deptMap.put(department.getId(), department);
        }
        // 收集一级数据
        for (Department deptTmp : depts) {
            if(deptTmp.getParentId() == null){
                deptTree.add(deptTmp);
            }else {
                // 对应一级数据的下级数据
                Department department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }
        // 返回数据信息
        return deptTree;
    }
}
